<?php
	ini_set("html_errors", "0");
	
	
	$url_site = "http://" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["SCRIPT_NAME"]) . "/";
	
	if (strstr($url_site, '//'))
	{
		$url_site = str_replace('/js', '/upload', $url_site);
		$url_site = str_replace('//', '/', $url_site);
		$url_site = str_replace(':/', '://', $url_site);
	}

	if (!isset($_FILES[$_POST["fichierupload"]]) || !is_uploaded_file($_FILES[$_POST["fichierupload"]]["tmp_name"]) || $_FILES[$_POST["fichierupload"]]["error"] <> 0) 
		die("ERROR : invalid upload");
	
	copy($_FILES[$_POST["fichierupload"]]["tmp_name"], '../upload/' . $_FILES[$_POST["fichierupload"]]["name"]);
	
	$extimages = array(
		'jpg',
		'jpeg',
		'gif',
		'png',
		'bmp'
	);
	
	$tabext = explode('.', $_FILES[$_POST["fichierupload"]]["name"]);
	$ext = $tabext[sizeof($tabext) - 1];
	
	if (in_array($ext, $extimages))
	{
		list($width, $height, $type, $attr) = getimagesize('../upload/' . $_FILES[$_POST["fichierupload"]]["name"]);
		if ($width > 150)
		{
			$nwidth = 150;
			$rapport = $width / $nwidth;
			$nheight = $height * $rapport;
		}
		else
			$nwidth = $width;
			$nheight = $height;
		
		$js = '<script>window.parent.document.getElementById(\'wait_'.$_POST["fichierupload"].'\').style.display = \'none\'; window.parent.document.getElementById(\''.$_POST["fichierupload"].'_file\').value = "'.$_FILES[$_POST["fichierupload"]]["name"].'"; window.parent.document.getElementById(\'uploadform-temp-'.$_POST["fichierupload"].'\').style.height = "'.($nheight * 1.2).'";window.parent.document.getElementById(\'uploadform-temp-'.$_POST["fichierupload"].'\').style.height = "'.($nwidth * 1.2).'";window.parent.document.getElementById(\'resultupload_'.$_POST["fichierupload"].'\').innerHTML = document.body.innerHTML; window.parent.document.getElementById(\'resultupload_'.$_POST["fichierupload"].'\').style.display = "block";</script>';
		die('<body><a href="' . $url_site . $_FILES[$_POST["fichierupload"]]["name"] . '" target="_blank"><img src="' . $url_site . $_FILES[$_POST["fichierupload"]]["name"] . '" width="'.$nwidth.'" style="border: none;" /></a></body>' . $js);
	}
	else
	{
		$js = '<script>window.parent.document.getElementById(\'wait_'.$_POST["fichierupload"].'\').style.display = \'none\'; window.parent.document.getElementById(\''.$_POST["fichierupload"].'_file\').value = "'.$_FILES[$_POST["fichierupload"]]["name"].'"; window.parent.document.getElementById(\'uploadform-temp-'.$_POST["fichierupload"].'\').style.height = 1000; window.parent.document.getElementById(\'uploadform-temp-'.$_POST["fichierupload"].'\').style.height = 100;window.parent.document.getElementById(\'resultupload_'.$_POST["fichierupload"].'\').innerHTML = document.body.innerHTML; window.parent.document.getElementById(\'resultupload_'.$_POST["fichierupload"].'\').style.display = "block";</script>';
		die('<body><a href="' . $url_site . $_FILES[$_POST["fichierupload"]]["name"] . '" target="_blank">' . $_FILES[$_POST["fichierupload"]]["name"] . '</a></body>' . $js);
	}
	
?>